#!/usr/bin/perl
################################################################################
#    Ogame.pm Perl Ogame BOT
#    Copyright (C) 2008  <ogame.pm@gmail.com>
# 
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
################################################################################

use strict;
use DBI;
use Data::Dumper;
use Getopt::Long;
$|=1;

my %rapid_fire; my %param; my @fleet; my @def; my %F; my %D; my $i; my $rand;
my $z;my @A;my @D;my $round;my $cible;my $max_structure;my $bouclier;my $attaque;my $structure;my $rf;
my ($_a_pt,$_a_gt,$_a_cle,$_a_clo,$_a_croi,$_a_colo,$_a_sat,$_a_recycl,$_a_vb,$_a_sonde,$_a_EDM,$_a_bomb,$_a_destr,$_a_traqueur);
my ($_d_pt,$_d_gt,$_d_cle,$_d_clo,$_d_croi,$_d_colo,$_d_sat,$_d_recycl,$_d_vb,$_d_sonde,$_d_EDM,$_d_bomb,$_d_destr,$_d_traqueur);
my ($_d_lm,$_d_lle,$_d_llo,$_d_gauss,$_d_plasma,$_d_ions,$_d_PB,$_d_GB);
my ($A_metal,$A_cristal,$A_deuterium);
my ($D_metal,$D_cristal,$D_deuterium);
my ($_date,$_time,$_dbname);
my ($_a_armes,$_a_bouclier,$_a_protection);

#rf{A}{B}= 100 - (100/rfA-B)
my $result = GetOptions(
								"db=s" 					=> \$_dbname,
								"date=s"					=> \$_date,
								"time=s"					=> \$_time,
								"armes=n" 				=> \$_a_armes,
								"bouclier=n" 			=> \$_a_bouclier,
								"protection=n"			=> \$_a_protection,
								"pt=n" 					=> \$_a_pt,
								"gt=n" 					=> \$_a_gt,
								"cle=n" 					=> \$_a_cle,
								"clo=n" 					=> \$_a_clo,
								"croi=n" 				=> \$_a_croi,
								"recycl=n" 				=> \$_a_recycl,
								"vb=n" 					=> \$_a_vb,
								"sat=n" 					=> \$_a_sat,
								"sonde=n" 				=> \$_a_sonde,
								"bomb=n" 				=> \$_a_bomb,
								"destr=n" 				=> \$_a_destr,
								"EDM=n" 					=> \$_a_EDM,
								"traqueur=n"			=> \$_a_traqueur,
								"d_lm=n" 				=> \$_d_lm,
								"d_lle=n" 				=> \$_d_lle,
								"d_llo=n" 				=> \$_d_llo,
								"d_gauss=n" 			=> \$_d_gauss,
								"d_plasma=n" 			=> \$_d_plasma,
								"d_PB=n" 				=> \$_d_PB,
								"d_GB=n" 				=> \$_d_GB,
								"d_pt=n" 				=> \$_d_pt,
								"d_gt=n" 				=> \$_d_gt,
								"d_cle=n" 				=> \$_d_cle,
								"d_clo=n" 				=> \$_d_clo,
								"d_croi=n" 				=> \$_d_croi,
								"d_recycl=n" 			=> \$_d_recycl,
								"d_vb=n" 				=> \$_d_vb,
								"d_sat=n" 				=> \$_d_sat,
								"d_sonde=n" 			=> \$_d_sonde,
								"d_bomb=n" 				=> \$_d_bomb,
								"d_destr=n" 			=> \$_d_destr,
								"d_EDM=n" 				=> \$_d_EDM,
								"d_traqueur=n"			=> \$_d_traqueur,
								);

### Initialisations
my %rapid_fire= (	PT => { ESP => 80.0, SAT => 80.0 },
					GT => { ESP => 80.0, SAT => 80.0 },
					CLE => { ESP => 80.0, SAT => 80.0 },
					CLO => { ESP => 80.0, SAT => 80.0 },
					CROI => { ESP => 80.0, SAT => 80.0, 
					CLE => 83.3, LM => 90.0 },
					VB => { ESP => 80.0, SAT => 80.0 },
					COL => { ESP => 80.0, SAT => 80.0 },
					REC => { ESP => 80.0, SAT => 80.0 },
					BOM => { ESP => 80.0, SAT => 80.0, 
								LM => 95.0, LLE => 95.0, 
								LLO => 90.0, IONS => 90.0},
					DESTR => { ESP => 80.0, SAT => 80.0,
								LLE => 90.0 },
					EDM => { ESP => 99.92, SAT => 99.92,
								PT => 99.6, GT => 99.6,
								CLE => 99.5, CLO => 99.0,
								CROI => 97.0, VB => 96.6,
								COL => 99.6, REC => 99.6,
								BOM => 96.0, DESTR => 80.0,
								LM => 99.5, LLE => 99.5,
								LLO => 99.0, DGC => 98.0, 
								IONS => 99.0}
);


$param{PT}{structure}=400; $param{PT}{bouclier}= 10; $param{PT}{attaque}= 5; $param{PT}{fret}= 5000; $param{PT}{vitesse}= 5000; $param{PT}{conso}= 10;
$param{GT}{structure}=1200; $param{GT}{bouclier}= 25; $param{GT}{attaque}= 5; $param{GT}{fret}= 25000; $param{GT}{vitesse}= 7500; $param{GT}{conso}= 50;
$param{CLE}{structure}=400; $param{CLE}{bouclier}= 10; $param{CLE}{attaque}= 50; $param{CLE}{fret}= 50; $param{CLE}{vitesse}= 12500; $param{CLE}{conso}= 20;
$param{CLO}{structure}=1000; $param{CLO}{bouclier}= 25; $param{CLO}{attaque}= 150; $param{CLO}{fret}= 100; $param{CLO}{vitesse}= 10000; $param{CLO}{conso}= 75;
$param{CROI}{structure}=2700; $param{CROI}{bouclier}= 50; $param{CROI}{attaque}= 400; $param{CROI}{fret}= 800; $param{CROI}{vitesse}= 15000; $param{CROI}{conso}= 300;
$param{VB}{structure}=6000; $param{VB}{bouclier}= 200; $param{VB}{attaque}= 1000; $param{VB}{fret}= 1500; $param{VB}{vitesse}= 10000; $param{VB}{conso}= 500;
$param{COLO}{structure}=3000; $param{COLO}{bouclier}= 100; $param{COLO}{attaque}= 50; $param{COLO}{fret}= 7500; $param{COLO}{vitesse}= 2500; $param{COLO}{conso}= 1000;
$param{REC}{structure}=1600; $param{REC}{bouclier}= 10; $param{REC}{attaque}= 1; $param{REC}{fret}= 20000; $param{REC}{vitesse}= 2000; $param{REC}{conso}= 300;
$param{SONDE}{structure}=100; $param{SONDE}{bouclier}= 0.01; $param{SONDE}{attaque}=  0.01; $param{SONDE}{fret}= 5; $param{SONDE}{vitesse}= 100000000; $param{SONDE}{conso}= 1;
$param{BOMB}{structure}=7500; $param{BOMB}{bouclier}= 500; $param{BOMB}{attaque}= 1000; $param{BOMB}{fret}= 500; $param{BOMB}{vitesse}= 4000; $param{BOMB}{conso}= 1000;
$param{SAT}{structure}=200; $param{SAT}{bouclier}= 1; $param{SAT}{attaque}= 1; $param{SAT}{fret}= 0; $param{SAT}{vitesse}= 0; $param{SAT}{conso}= 0;
$param{DESTR}{structure}=11000; $param{DESTR}{bouclier}= 500; $param{DESTR}{attaque}= 2000; $param{DESTR}{fret}= 2000; $param{DESTR}{vitesse}= 5000; $param{DESTR}{conso}= 1000;
$param{EDM}{structure}=900000; $param{EDM}{bouclier}= 50000; $param{EDM}{attaque}= 200000; $param{EDM}{fret}= 1000000; $param{EDM}{vitesse}= 100; $param{EDM}{conso}= 10;
$param{TRAQU}{structure}=8500; $param{TRAQU}{bouclier}=70000; $param{TRAQU}{attaque}=400; $param{TRAQU}{fret}=700; $param{TRAQU}{vitesse}=750; $param{TRAQU}{conso}=10000;
$param{LM}{structure}=200; $param{LM}{bouclier}= 20; $param{LM}{attaque}= 80; $param{LM}{fret}= 0; $param{LM}{vitesse}= 0; $param{LM}{conso}= 0;
$param{LLE}{structure}=200; $param{LLE}{bouclier}= 25; $param{LLE}{attaque}= 100; $param{LLE}{fret}= 0; $param{LLE}{vitesse}= 0; $param{LLE}{conso}= 0;
$param{LLO}{structure}=800; $param{LLO}{bouclier}= 100; $param{LLO}{attaque}= 250; $param{LLO}{fret}= 0; $param{LLO}{vitesse}= 0; $param{LLO}{conso}= 0;
$param{GAUSS}{structure}=3500; $param{GAUSS}{bouclier}= 200; $param{GAUSS}{attaque}= 1100; $param{GAUSS}{fret}= 0; $param{GAUSS}{vitesse}= 0; $param{GAUSS}{conso}= 0;
$param{IONS}{structure}=800; $param{IONS}{bouclier}= 500; $param{IONS}{attaque}= 150; $param{IONS}{fret}= 0; $param{IONS}{vitesse}= 0; $param{IONS}{conso}= 0;
$param{PLASMA}{structure}=10000; $param{PLASMA}{bouclier}= 300; $param{PLASMA}{attaque}= 3000; $param{PLASMA}{fret}= 0; $param{PLASMA}{vitesse}= 0; $param{PLASMA}{conso}= 0;
$param{PB}{structure}=2000; $param{PB}{bouclier}= 2000; $param{PB}{attaque}= 1; $param{PB}{fret}= 0; $param{PB}{vitesse}= 0; $param{PB}{conso}= 0;
$param{GB}{structure}=10000; $param{GB}{bouclier}= 10000; $param{GB}{attaque}= 1; $param{GB}{fret}= 0; $param{GB}{vitesse}= 0; $param{GB}{conso}= 0;

#prix
$param{PT}{metal}=2000; $param{PT}{cristal}=2000;
$param{GT}{metal}=6000; $param{GT}{cristal}=6000;
$param{CLE}{metal}=3000; $param{CLE}{cristal}=1000;
$param{CLO}{metal}=6000; $param{CLO}{cristal}=4000;
$param{CROI}{metal}=20000; $param{CROI}{cristal}=7000; $param{CROI}{deuterium}=2000;
$param{VB}{metal}=45000; $param{VB}{cristal}=15000;
$param{COLO}{metal}=10000; $param{COLO}{cristal}=20000;$param{COLO}{deuterium}=10000;
$param{REC}{metal}=10000; $param{REC}{cristal}=6000;$param{REC}{deuterium}=2000;
$param{SONDE}{cristal}=1000;
$param{BOMB}{metal}=50000; $param{BOMB}{cristal}=25000;$param{BOMB}{deuterium}=15000;
$param{SAT}{metal}=0;$param{SAT}{cristal}=2000;$param{SAT}{deuterium}=500;
$param{DESTR}{metal}=60000; $param{DESTR}{cristal}=50000;$param{DESTR}{deuterium}=15000;
$param{EDM}{metal}=5000000; $param{EDM}{cristal}=4000000;$param{EDM}{deuterium}=1000000;
$param{TRAQU}{metal}=30000; $param{TRAQU}{cristal}=40000;$param{TRAQU}{deuterium}=15000;

$param{PLASMA}{metal}=50000;$param{PLASMA}{cristal}=50000; $param{PLASMA}{deuterium}=30000;
$param{LM}{metal}=2000;$param{LM}{crital}=0;
$param{LLE}{metal}=1500;$param{LLE}{cristal}=500;
$param{LLO}{metal}=6000;$param{LLO}{cristal}=2000;
$param{GAUSS}{metal}=20000;$param{GAUSS}{cristal}=15000;$param{GAUSS}{deuterium}=2000;
$param{IONS}{metal}=2000;$param{IONS}{cristal}=6000;
$param{PB}{metal}=10000;$param{PB}{cristal}=10000;
$param{GB}{metal}=50000;$param{GB}{cristal}=50000;


$F{PT}=$_a_pt; $F{GT}=$_a_gt; $F{CLE}=$_a_cle; $F{CLO}=$_a_clo; $F{CROI}=$_a_croi; $F{VB}=$_a_vb;
$F{COLO}=$_a_colo ;$F{REC}=$_a_recycl ;$F{SONDE}=$_a_sonde ;$F{BOMB}=$_a_bomb;$F{SAT}=$_a_sat;
$F{DESTR}=$_a_destr;$F{EDM}=$_a_EDM;$F{TRAQU}=$_a_traqueur;


@fleet=qw(PT GT CLE CLO CROI VB COLO REC SONDE BOMB SAT DESTR EDM TRAQU);
@def  =qw(PT GT CLE CLO CROI VB COLO REC SONDE BOMB SAT DESTR EDM TRAQU LM LLE LLO GAUSS IONS PLASMA PB GB);

if ( !defined($_a_armes) || !defined($_a_bouclier) || !defined($_a_protection)){
	print("technos attaquant undefined !!! \n");exit -1;
}
if ( !defined($_dbname) || !defined($_date) ){
	print("db or date undefined !!! \n");exit -1;
}

my $simulations=10;
my ($A_armes, $A_bouclier, $A_protection)=($_a_armes,$_a_bouclier,$_a_protection);
my ($D_armes, $D_bouclier, $D_protection)=(16,16,16); #au cas ou ...
my $dbh;
my $id;

$dbh = DBI->connect("dbi:SQLite:dbname=$_dbname","","",{ AutoCommit => 0 }) or &printerr('init_db: ',$dbh->errstr);
my $result=$dbh->selectall_arrayref('pragma user_version') or &printerr('init_db: ',$dbh->errstr);
if ($result && ($result->[0][0] != 9)) {	
	print "bad version\n";
	exit -1;
}
my $total=0;
my @RESULTS; #[0]: 
calc();
#print "GT      coord      pertes\n";
for (my $i=0;$i<@RESULTS;$i++){
	if (($RESULTS[$i][0]==100) and ($RESULTS[$i][3]<40000)) {
		print ," coordonees: \n",$RESULTS[$i][5];
#		print $RESULTS[$i][0]," victoires\n";
#		print $RESULTS[$i][1]," defaites\n";
#		print $RESULTS[$i][2]," nuls\n";
#		print $RESULTS[$i][3]," de perte pour attaquant\n";
		print int(($RESULTS[$i][4])/10000)+1,"      ",$RESULTS[$i][5],"      ",$RESULTS[$i][3],"\n";
	}
}

sub calc(){
my $n;
my $r=$dbh->selectall_hashref('select id,date,met,cri,deut,coord,armes,protection,bouclier,lm,lle,llo,gauss,ions,plasma,gb,pb,pt,gt,cle,clo,croi,vb,destr,bomb,edm from espionnage where date > '.$_date.' and tech_found=1','id') 
	or &printerr('init_db: ',$dbh->errstr);
#print @$r;
foreach $id (keys %$r) {
	my @results;
	my $def_ratio=(($r->{$id}->{lm}*2) + ($r->{$id}->{lle}*2) + ($r->{$id}->{llo}*8) + ($r->{$id}->{gauss}*4) + 
		($r->{$id}->{ions}*8) + ($r->{$id}->{plasma}*130) + ($r->{$id}->{pb}*10) + ($r->{$id}->{gb}*20) + 
		($r->{$id}->{cle}*4) + ($r->{$id}->{clo}*10) + ($r->{$id}->{croi}*30) + ($r->{$id}->{vb}*60) + 
		($r->{$id}->{destr}*120) + ($r->{$id}->{bomb}*100) + ($r->{$id}->{edm}*10000) + ($r->{$id}->{traq}*80)); 
	$_d_lm=$r->{$id}->{lm};
	$_d_lle=$r->{$id}->{lle};
	$_d_llo=$r->{$id}->{llo};
	$_d_gauss=$r->{$id}->{gauss};
	$_d_ions=$r->{$id}->{ions};
	$_d_plasma=$r->{$id}->{plasma};
	$_d_PB=$r->{$id}->{pb};
	$_d_GB=$r->{$id}->{gb};
	$_d_pt=$r->{$id}->{pt};
	$_d_gt=$r->{$id}->{gt};
	$_d_cle=$r->{$id}->{cle};
	$_d_clo=$r->{$id}->{clo};
	$_d_croi=$r->{$id}->{croi};
	$_d_vb=$r->{$id}->{vb};
	$_d_bomb=$r->{$id}->{bomb};
	$_d_destr=$r->{$id}->{destr};
	$_d_EDM=$r->{$id}->{edm};
	$_d_traqueur=$r->{$id}->{traq};

	$total= ($r->{$id}->{met} + $r->{$id}->{cri} + $r->{$id}->{deut} );
	$D_armes=$r->{$id}->{armes};
	$D_bouclier=$r->{$id}->{bouclier};
	$D_protection=$r->{$id}->{protection};
	
	$D{PT}=$_d_pt; $D{GT}=$_d_gt; $D{CLE}=$_d_cle; $D{CLO}=$_d_clo; $D{CROI}=$_d_croi; $D{VB}=$_d_vb;
	$D{COLO}=$_d_colo ;$D{REC}=$_d_recycl ;$D{SONDE}=$_d_sonde ;$D{BOMB}=$_d_bomb;$D{SAT}=$_d_sat;
	$D{DESTR}=$_d_destr;$D{EDM}=$_d_EDM;$D{TRAQU}=$_d_traqueur;
	$D{LM}=$_d_lm; $D{LLE}=$_d_lle; $D{LLO}=$_d_llo; $D{GAUSS}=$_d_gauss; $D{PLASMA}=$_d_plasma; $D{PB}=$_d_GB; $D{GB}=$_d_GB;

	#print "\n\nID: $id is $r->{$id}->{date}  total: $total def ratio: $def_ratio recherches: $D_armes $D_bouclier $D_protection\n";
#foreach my $row_ref (%$r) {
#	print  (join(' ',@$row_ref));
#	print  (join(' ',%$row_ref));

	$A_armes 		= (10 + $A_armes)/10;
	$A_bouclier 	= (10 + $A_bouclier)/10;
	$A_protection 	= (10 + $A_protection)/10;
	$D_armes 		= (10 + $D_armes)/10;
	$D_bouclier 	= (10 + $D_bouclier)/10;
	$D_protection 	= (10 + $D_protection)/10;

	for (my $simu=0; $simu<$simulations; $simu++) {
		my @A;
		my @D;
		my @DD;
		my @AA;
		($A_metal,$A_cristal,$A_deuterium,$D_metal,$D_cristal,$D_deuterium)=(0,0,0,0,0,0);
		# Initialisation du tableau attaquant
		my $j=0;
		for ($z=0;$z<@fleet;$z++){
			for ($i=0;$i< int($F{$fleet[$z]});$i++){
				$A[$j][0]=($param{$fleet[$z]}{structure} * $A_protection);
				$A[$j][1]=($param{$fleet[$z]}{bouclier} * $A_bouclier);
				$A[$j][2]=($param{$fleet[$z]}{attaque} * $A_armes);
				$A[$j][3]=$fleet[$z];
				$A[$j][4]=0;
				$A[$j][5]=$param{$fleet[$z]}{metal};$A_metal=$A_metal+$A[$j][5];
				$A[$j][6]=$param{$fleet[$z]}{cristal};$A_cristal=$A_cristal+$A[$j][6];
				$A[$j][7]=$param{$fleet[$z]}{deuterium};$A_deuterium=$A_deuterium+$A[$j][7];
				$j++;
			}
		}

	# Initialisation du tableau d?fenseur
		$j=0;
		for ($z=0;$z<@def;$z++){
			for ($i=0;$i< int($D{$def[$z]});$i++){
				$D[$j][0]=($param{$def[$z]}{structure} * $D_protection);
				$D[$j][1]=($param{$def[$z]}{bouclier} * $D_bouclier);
				$D[$j][2]=($param{$def[$z]}{attaque} * $D_armes);
				$D[$j][3]=$def[$z];
				$D[$j][4]=0;
				$D[$j][5]=$param{$def[$z]}{metal}	 ;$D_metal=$D_metal+$D[$j][5];
				$D[$j][6]=$param{$def[$z]}{cristal}	 ;$D_cristal=$D_cristal+$D[$j][6];
				$D[$j][7]=$param{$def[$z]}{deuterium};$D_deuterium=$D_deuterium+$D[$j][7];
				$j++;
			}
		}
#print "cout attaquant: $A_metal $A_cristal $A_deuterium \n";
#print "cout defenseur: $D_metal $D_cristal $D_deuterium \n";
### Calcul de la bataille
		my $finish=0;
	
		for ($round=0; $round<6 and (@D && @A); $round++){
#	print "ROUND: ",$round+1,"\n";

### attaquant vs defender
#	print "Attaquants (".@A.") tirent sur D?fenseurs (".@D.")\n";
			for ($i=0;$i<@A;$i++){
				$cible=int(rand((@D)));	
				$max_structure=$param{$D[$cible][3]}{structure};
				$structure=$D[$cible][0];
				$bouclier =$D[$cible][1];
				$attaque  =$A[$i][2];
				if($bouclier > 0){
					if($attaque/$bouclier < 0.01) {
						$attaque = 0;
					}
					else {
						$bouclier = $bouclier - $attaque;
						$attaque  = -$bouclier;
						if($bouclier < 0) {
							$bouclier = 0;
						}
						if($attaque < 0){
							 $attaque = 0;
						}
					}
				}
				$structure = $structure - $attaque;
				$D[$cible][0]=$structure;
				$D[$cible][1]=$bouclier;
				if($structure < 0) {
					$structure = 0;
					$D[$cible][4]=1;
				}
				if($structure < (0.7 * $max_structure)) {
					if((rand(100) > (100.0 * $structure/$max_structure))) {
						$D[$cible][4]=1;
					}
				}
				if (defined($rapid_fire{$A[$i][3]}{$D[$cible][3]})) {
					$rf = 100*$rapid_fire{$A[$i][3]}{$D[$cible][3]};
					$rand=int(rand(10000));
					if ($rand < $rf){
						#print "il y a RF possible\n";
						$i--;
					}
				}
			}
# 	defender vs attaquant
#	print "Defenseurs (".@A.") tirent sur Attaquants (".@D.")\n";
			for ($i=0;$i<@D;$i++){
				$cible=int(rand((@A)));	
				$max_structure=$param{$A[$cible][3]}{structure};
				$structure=$A[$cible][0];
				$bouclier =$A[$cible][1];
				$attaque  =$D[$i][2];
				if($bouclier > 0){
					if($attaque/$bouclier < 0.01) {
						$attaque = 0;
					}
					else {
						$bouclier = $bouclier - $attaque;
						$attaque  = -$bouclier;
						if($bouclier < 0) {
							$bouclier = 0;
						}
						if($attaque < 0){
							 $attaque = 0;
						}
					}
				}
				$structure = $structure - $attaque;
				$A[$cible][0]=$structure;
				$A[$cible][1]=$bouclier;
				if($structure < 0) {
					$structure = 0;
					$A[$cible][4]=1;
				}
				if($structure < (0.7 * $max_structure)) {
					if((rand(100) > (100.0 * $structure/$max_structure))) {
						$A[$cible][4]=1;
					}
				}
				if (defined($rapid_fire{$D[$i][3]}{$A[$cible][3]})) {
					$rf = 100*$rapid_fire{$D[$i][3]}{$A[$cible][3]};
					$rand=int(rand(10000));
					if ($rand < $rf){
						#print "il y a RF possible\n";
						$i--;
					}
				}
			}
			@AA=();
			@DD=();
			$j=0;
			for ($i=0;$i<@A;$i++){
				if (defined($A[$i][4]) && $A[$i][4]!=1){
					$A[$i][0]=$param{$A[$i][3]}{structure};
					push @AA,$A[$i];
					$j++;
				}
			}
			@A=$j;
			@A=();
			@A=@AA;
			$j=0;
			for ($i=0;$i<@D;$i++){
				if (defined($D[$i]) and ($D[$i][4]==0)){
					$D[$i][0]=$param{$D[$i][3]}{structure};
					push(@DD,$D[$i]);
					$j++;
				}
			}
			@D=$j;
			@D=();
			@D=@DD;
		}
		#print ".";
	


	my %resA;
	my %resD;
	my	 ($sum_A_metal,$sum_A_cristal,$sum_A_deuterium)=(0,0,0);
	my ($sum_D_metal,$sum_D_cristal,$sum_D_deuterium)=(0,0,0);
	for (my $i=0; $i<@A; $i++) { 
		$resA{$A[$i][3]}++; 
		$sum_A_metal+=$A[$i][5];	
		$sum_A_cristal+=$A[$i][6];	
		$sum_A_deuterium+=$A[$i][7];	
	}
	for (my $i=0; $i<@D; $i++) { 
		$resD{$D[$i][3]}++; 
		$sum_D_metal+=$D[$i][5];	
		$sum_D_cristal+=$D[$i][6];	
		$sum_D_deuterium+=$D[$i][7];	
	}
	push @results, [ $round+1, [@A], [@D], \%resA, \%resD, $sum_A_metal, $sum_A_cristal, $sum_A_deuterium, $sum_D_metal, $sum_D_cristal, $sum_D_deuterium];
	}

	$simulations=100;

#	print "\nreste attaquant: M-> ",$results[0][5]," C-> ",$results[0][6]," D-> ",$results[0][7],"\n";
#	print "reste defenseur: M-> ",$results[0][8]," C-> ",$results[0][9]," D-> ",$results[0][10],"\n";
#	print"\n";

#	print "\n";
	my ($min_round,$max_round,$mean_round)=(6,0,0);
	my ($vicA, $vicD, $nul)=(0,0,0);
	my %mean_A;
	my %mean_D;
	my ($mean_A_metal,$mean_A_cristal,$mean_A_deuterium)=(0,0,0);
	my ($mean_D_metal,$mean_D_cristal,$mean_D_deuterium)=(0,0,0);

	for (my $i=0; $i<@results; $i++) {
		$min_round=($min_round<$results[$i][0])?$min_round:$results[$i][0];
		$max_round=($max_round>$results[$i][0])?$max_round:$results[$i][0];
		$mean_round+=$results[$i][0];
	
		$mean_A_metal+=$results[$i][5];
		$mean_A_cristal+=$results[$i][6];
		$mean_A_deuterium+=$results[$i][7];
		$mean_D_metal+=$results[$i][8];
		$mean_D_cristal+=$results[$i][9];
		$mean_D_deuterium+=$results[$i][10];
	
		if (scalar(@{$results[$i][1]}) == 0){
			$vicD++;
		} elsif (scalar(@{$results[$i][2]}) == 0){
			$vicA++;
		} else { $nul++; }
		my %A=%{$results[$i][3]};
		foreach my $key (keys %A) {
			$mean_A{$key}+=$A{$key};
		}
		my %D=%{$results[$i][4]};
		foreach my $key (keys %D) {
			$mean_D{$key}+=$D{$key};
		}
	}
	
	$mean_round=$mean_round/scalar(@results);
	$mean_A_metal=$mean_A_metal/scalar(@results);
	$mean_A_cristal=$mean_A_cristal/scalar(@results);
	$mean_A_deuterium=$mean_A_deuterium/scalar(@results);
	$mean_D_metal=$mean_D_metal/scalar(@results);
	$mean_D_cristal=$mean_D_cristal/scalar(@results);
	$mean_D_deuterium=$mean_D_deuterium/scalar(@results);

#	printf "Nombre de tours: %d\n", $simulations;
#	printf "Rounds:\tmin %d\tmax %d\tmean %.2f\n", $min_round-1, $max_round-1, $mean_round-1;
#	printf "%d victoires attaquant, %d victoires defenseur, %d matchs nuls\n", $vicA, $vicD, $nul;

	my $reste_A_M=$A_metal-$mean_A_metal; my $reste_A_C=$A_cristal-$mean_A_cristal; my $reste_A_D=$A_deuterium-$mean_A_deuterium;
	my $reste_D_M=$D_metal-$mean_D_metal; my $reste_D_C=$D_cristal-$mean_D_cristal; my $reste_D_D=$D_deuterium-$mean_D_deuterium;
	my $reste_A=$reste_A_M+$reste_A_C+$reste_A_D;
	my $reste_D=$reste_D_M+$reste_D_C+$reste_D_D;
#	printf "Pertes attaquant: M-> %d C-> %d D-> %d total-> %d\n", $reste_A_M,$reste_A_C,$reste_A_D,$reste_A,"\n";
#	printf "Pertes defenseur : M-> %d C-> %d D-> %d total-> %d\n", $reste_D_M,$reste_D_C,$reste_D_D,$reste_D,"\n";

#	print "_"x25,"\n","Attaquant: \n";
	foreach my $key (keys %mean_A) {
#		printf("\t%s %f\n", $key, $mean_A{$key}/$simulations);
	}
#	print "_"x25,"\n","Defenseur: \n";
	foreach my $key (keys %mean_D) {
#		printf("\t%s %f\n", $key, $mean_D{$key}/$simulations);
	}
	$RESULTS[$n][0]=$vicA;
	$RESULTS[$n][1]=$vicD;
	$RESULTS[$n][2]=$nul;
	$RESULTS[$n][3]=$reste_A;
	$RESULTS[$n][4]=$total;
	$RESULTS[$n][5]=$r->{$id}->{coord};
	$n++;
}
$dbh->disconnect();
}

